#!/usr/bin/env bash

export PATH=/home/scmtools/buildkit/node/node_8.1.3/bin:$PATH

node -v

cd $(readlink -f $(dirname $0))

function get_release_version() {
  module="$1"

  info=$(curl -s "http://agile.baidu.com/api/agile/getReleaseInfoOutputUrl?module=${module}")
  version=$(python -c "import json; result=json.loads('${info}'); print result['releaseVersion']")
  echo $version
}

function getprod() {
  module="$1"
  version="$2"
  output="$3"

  basename=$(basename "${module}")
  tag=$(echo "${version}" | sed 's/\./-/g')
  tag="${basename}_${tag}_PD_BL"

  wget -q ftp://getprod@product.scm.baidu.com//data/prod-64/${module}/${tag}/output/${output} \
    --user getprod \
    --password getprod
}

function compile() {
  COMMON_VERSION=$(cat common_version)

  # 读取node_modules版本号
  if [ -z "${COMMON_VERSION}" ]; then
      # 直接拉取最新版本
      COMMON_VERSION=$(get_release_version "baidu/bce-iot/fe-panda-common")
  fi


  echo $COMMON_VERSION | tee common_version

  getprod "baidu/bce-iot/fe-panda-common" "${COMMON_VERSION}" "development/node_modules.tgz"
  tar zxf node_modules.tgz


  # compile
  npm run compile

  # 清理开发环境的node依赖
  rm -rf node_modules
  rm -rf node_modules.tgz

  mkdir -p output/bin
  mkdir -p output/conf

  cp -r `ls -a | grep -E "^(dist|pm2.json|server|startup.sh|common|views|.babelrc|client|cmrh.conf.js)$"` output/bin
  cp deploy/start.sh output/bin
  cp deploy/stop.sh output/bin
  cp deploy/deployToBos.js output/bin
  cp deploy/bcedeploywhitelist.txt output

  # 将生产环境依赖包放入output
  getprod "baidu/bce-iot/fe-panda-common" "${COMMON_VERSION}" "production/node_modules.tgz"
  mv node_modules.tgz output/bin/

  # 生成version.txt文件
  version=$(sed -n '1p' BCLOUD  |  awk -F '"' '{print $2}')
  branch=$(git rev-parse --short HEAD)
  echo $version.$branch > output/bin/client/assets/version.txt

  # 更新当前版本号
  curl -s -X POST -d "panda=${COMMON_VERSION}" http://nmg01-hpc-w1139.nmg01.baidu.com:8183/api/panda/version?token=ee61052329945555542050a5fadd6348

  cd output/bin/
  tar zcf dist.tgz dist
  rm -rf dist
  tar zcf client.tgz client
  rm -rf client
  tar zcf common.tgz common
  rm -rf common
  tar zcf views.tgz views
  rm -rf views
  cd ../../
}

rm -rf output
mkdir output

CHANGED_FILES=$(git diff --name-only HEAD~ HEAD)

echo $CHANGED_FILES

CONTAINED=$(echo "$CHANGED_FILES" | grep "package.json")

CURRENT_COMMON_VERSION=$(curl -s http://nmg01-hpc-w1139.nmg01.baidu.com:8183/api/panda/version)

LATEST_COMMON_VERSION=$(get_release_version "baidu/bce-iot/fe-panda-common")

echo "Current common version: $CURRENT_COMMON_VERSION; Latest common version: $LATEST_COMMON_VERSION"

# 只有在COMMON版本未更新的时候进行编译
if [ "$CONTAINED" != "" -a "$CURRENT_COMMON_VERSION" == "$LATEST_COMMON_VERSION" ];then
  curl -s "http://agile.baidu.com/api/agile/pipeline/build?user=chenqiang03&token=b86818e6-12ca-40a5-82d3-3b46f3d983b4&pipeline=456684"
  echo -e "\nStopped pipeline by script! \nCommon modules start building..."
  # 终止后续的流水，agile暂时未提供优雅的方法，粗暴搞下
  exit 1
else
  compile
fi
